/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.toggletest;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.options.BaseConfigurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import java.util.List;

class ToggleTestConfigurable extends BaseConfigurable implements ProjectComponent {
    private Project project;
    private ToggleTableModel tableModel;
    private ConfigurationPanel configurationPanel;

    public ToggleTestConfigurable(Project project) {
        this.project = project;
    }

    public String getDisplayName() {
        return "ToggleTest";
    }

    public Icon getIcon() {
        return null;
    }

    public String getHelpTopic() {
        return null;
    }

    public JComponent createComponent() {
        List toggles = ToggleTestConfiguration.getInstance(project).getToggles();
        tableModel = new ToggleTableModel(toggles);
        tableModel.addTableModelListener(new TableModelListener() {
            public void tableChanged(TableModelEvent tableModelEvent) {
                setModified(true);
            }
        });
        configurationPanel = new ConfigurationPanel(tableModel);
        return configurationPanel;
    }

    public void apply() throws ConfigurationException {
        configurationPanel.stopEditing();
        ToggleTestConfiguration.getInstance(project).setToggles(tableModel.getToggles());
    }

    public void reset() {
        tableModel.setToggles(ToggleTestConfiguration.getInstance(project).getToggles());
    }

    public void disposeUIResources() {
        tableModel = null;
        configurationPanel = null;
    }


    public void projectOpened() {
    }

    public void projectClosed() {
    }

    public String getComponentName() {
        return "ToggleTestConfigurable";
    }

    public void initComponent() {
    }

    public void disposeComponent() {
    }

}
